<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script src="js/jquery-1.11.3.min.js"></script>
<script type="text/javascript" src="js/bootstrap.min.js"></script>
<script type="text/javascript" src="js/page.js"></script>
<title>后台用户管理</title>
</head>
<style>
.permission {

}

.permission ul {
	list-style: none;
	height: 20px;
	margin-left: 50px;
}

.permission li {
	float: left;
	margin-left: 30px;
	height: 30px;
}

.permission p {
	float: left;
	
}

.permission span {
	height: 20px;
	line-height: 20px;
	margin-right: 3px;
}
</style>
<body>
<input type="hidden" name="admintype"
			value="${ sessionScope.adminType }"> <input type="hidden"
			name="permission" value="${ sessionScope.permission }">
	<jsp:include page="navigation.jsp"></jsp:include>
	<div class="container" style="margin-left: 190px; margin-bottom: 25px;">
		<div class="topbar-inner">
			<div class="myhead">
				<span class="crumb-name">后台用户管理</span>
			</div>
		</div>
		<div class="topbar-inner">
			<div class="search">
				<div class="input-group">
					<button type="button" id="add_btn" onclick="showalert_add()" class="btn btn-default" style="margin-left: 20px; margin-top: 1px;">新增</button>
				</div>
			</div>
		</div>
		<form id="pageForm" action="admininfo" method="post">
			<c:if test="${ page != null }">
				<input type="hidden" value="${ page.pageCount }" id="pageCount" name="pageCount" />
				<input type="hidden" value="${ page.pageIndex }" id="pageIndex" name="pageIndex" />
			</c:if>
		</form>




		<div style="margin: 20px 20px 20px 20px; text-align: center;">
			<table class="table table-bordered table-hover table-striped">
				<thead>
					<tr>
						<th>用户名</th>
						<th>操作</th>
					</tr>
				</thead>
				<c:forEach items="${ list }" var="l" varStatus="st">
					<tr>
						<td>${ l.user_name }</td>
						<td width="20%">
						<a href="javascript:" onclick="showalert_update(${ l.id },'${ l.user_name }','${ l.password }','${l.permission}',${l.type})">编辑</a> 
						<c:if test="${ l.user_name != 'admin' }">&emsp;|&emsp; <a href="javascript:" onclick="toDelete(${ l.id })">删除</a>
							</c:if>
<%-- 						&emsp;|&emsp;<a href="javascript:" onclick="toDelete(${ l.id })">修改权限</a> --%>
						</td>
					</tr>
				</c:forEach>
			</table>
		</div>
	</div>
</body>
<!-- 模态框（Modal） -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	<div class="modal-dialog">
		<div class="modal-content">
			<form action="adminedit" id="editorAdminForm" method="post">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
					<h4 class="modal-title" id="myModalLabel">用户添加</h4>
				</div>
				<div class="modal-body">
					<input type="hidden" id="modal_id" class="form-control" name="id" />
					<div id="user_name_id" style="width: 100%; height: 40px;">
						用户名：<input type="text" id="modal_user_name" class="form-control" name="add_adminname" />
					</div>
					<div style="width: 100%; height: 40px;">
						密&emsp;码：<input type="text" id="modal_password" class="form-control" name="add_password" />
					</div>
					<div id="permissiondiv" class="permission" style="">
					
						<div class="col-xs-8" style="width: 100%;padding-left: 0px;">
						管理员类型
							<select class="form-control input-xs" name="type" id="type"
								onchange="">
								<option value="0">普通管理员</option>
								<option value="1">超级管理员</option>
							</select>
						</div>
					<p>权限</p>
					<ul>
						<li><span>banner管理</span><input name="permission" type="checkbox"
							value="1" /></li>
						<li><span>用户管理</span><input name="permission" type="checkbox"
							value="2" /></li>
						<li><span>内容管理</span><input name="permission" type="checkbox"
							value="3" /></li>
					</ul>
					<div style="clear: both;"></div>
					<ul>
						<li><span>精选管理</span><input name="permission" type="checkbox"
							value="4" /></li>
						<li><span>充值管理</span><input name="permission" type="checkbox"
							value="5" /></li>
						<li><span>系统管理</span><input name="permission" type="checkbox"
							value="6" /></li>
					</ul>
<!-- 					<div style="clear: both;"></div> -->
<!-- 					<ul> -->
<!-- 						<li><span>汽车交易管理</span><input name="permission" -->
<!-- 							type="checkbox" value="7" /></li> -->
<!-- 						<li><span>推送管理</span><input name="permission" type="checkbox" -->
<!-- 							value="8" /></li> -->
<!-- 						<li><span>其他</span><input name="permission" type="checkbox" -->
<!-- 							value="9" /></li> -->
<!-- 					</ul> -->
<!-- 					<div style="clear: both;"></div> -->
<!-- 					<ul> -->
<!-- 						<li><span>门店服务管理</span><input name="permission" -->
<!-- 							type="checkbox" value="10" /></li> -->
<!-- 					</ul> -->
<!-- 					<div style="clear: both;"></div> -->
				</div>
					<div class="modal-footer">
						<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
						<button type="button" class="btn btn-primary" id="sub_btn" onclick="toAdd()">提交</button>
					</div>
					</div>
			</form>
		</div>
	</div>
</div>
<!-- /.modal -->
<script type="text/javascript">
$(function() {
    $('.submenu_5_1').addClass('submenu_native');
});

function showalert_add() {
    $("#myModal").modal('show');
    $('#user_name_id').css('display', 'block');
    var permission = $('input[name=permission]').val();
    var type = $('input[name=admintype]').val();
    if (type == 0) {
    	$('#permissiondiv').css('display', 'none');	
    }else{
		$('#permissiondiv').css('display', 'block');
	}
    
}

function showalert_update(id, user_name, password,permission,type) {
    $("#modal_id").val(id);
    $("#type").val(type);
    $('#user_name_id').css('display', 'none');
    $("#myModal").modal('show');
    $('#sub_btn').removeAttr('onclick');
    $('#sub_btn').attr('onclick', 'toEdit()');
    //var permission = $('input[name=permission]').val();
    var type = $('input[name=admintype]').val();
    if (type == 0) {
    	$('#permissiondiv').css('display', 'none');	
    }else{
		$('#permissiondiv').css('display', 'block');
	}
    var str = permission;
    var strArry = str.split(',');
    for(var i =0 ;i<strArray.length;i++)
    {
//         var temp = strArray[i];
//        if( $('input[name=permission]:checkbox').val())
        
    }
}

function toEdit() {
    var id = $('#modal_id').val();
    var add_password = $('input[name=add_password]').val().replace(/\s+/g, '');
    if (add_password != '' && add_password.length < 6) {
        alert('密码不能少于六位！');
        return false;
    }
    var type = $("#type").val();
    var flag = false;
    $('input[name=permission]:checkbox').each(function() { 
		if ($(this).prop('checked')) { 
			flag = true;
			} 
		});
    if (!flag) {
		alert("请至少选择一个权限！");
		return; 
	}
    $.ajax({
        type : 'post',
        url: "adminedit",
        //data : {password : add_password, id : id ,type:type,permission:permission},
        data: $('#editorAdminForm').serialize(),
        success : function (data) {
            console.log(data);
            if (data != null) {
                if (data.code == 0) {
                    alert('修改成功！');
                    window.location.reload();
                } else {
                    alert(data.message);
                }
                
            }
        }
    });
}


function toAdd() {
    var add_adminname = $('input[name=add_adminname]').val().replace(/\s+/g, '');
    if (add_adminname == '') {
        alert('用户名不能为空！');
        return false;
    }
    
    var add_password = $('input[name=add_password]').val().replace(/\s+/g, '');
    if (add_password == '') {
        alert('密码不能为空！');
        return false;
    }
    if (add_password.length < 6) {
        alert('密码不能少于六位！');
        return false;
    }
    var type = $("#type").val();
    var permission = $("#permission").val();
    var flag = false;
    $('input[name=permission]:checkbox').each(function() { 
		if ($(this).prop('checked')) { 
			flag = true;
			} 
		});
    if (!flag) {
		alert("请至少选择一个权限！");
		return; 
	}
    $.ajax({
        type : 'post',
        url: "adminedit",
//         data : {user_name : add_adminname, password : add_password ,type:type,permission:permission}, 
        data: $('#editorAdminForm').serialize(),
        success : function (data) {
            console.log(data);
            if (data != null) {
                if (data.code == 0) {
                    alert('添加成功！');
                    window.location.reload();
                } else {
                    alert(data.message);
                }
                
            }
        }
    });
}

function toDelete(id) {
    if (confirm("确定删除？")) {
        $.ajax({
            type : 'post',
            url: "admindelete",
            data : {id: id}, 
            success : function (data) {
                console.log(data);
                if (data != null) {
                    if (data.code == 0) {
                        alert('删除成功！');
                        window.location.reload();
                    } else {
                        alert("删除失败！");
                    }
                }
            }
        });
    }
}
</script>
</html>